import { createAction } from 'redux-actions'
import { UPDATELANDMARK } from '../types/landmark'
import { positioningRequest, getCityListRequest } from '../../service/city-controller'

export const positioning = createAction(UPDATELANDMARK, payload => {
  return new Promise(resolve => {
    getCityListRequest().then(({ data: dataList }) => {
      // 检测定位城市是否在支持城市列表中
      const { entry: entryList } = dataList
      positioningRequest(payload).then(({ data }) => {
        let entry = data && data.entry
        if (entry) {
          entry = entry.replace('市', '')
        }
        if (entry) {
          let isInCity = false
          for (let i in entryList) {
            if (entryList[i].cityName == entry) {
              isInCity = true
            }
          }
          if (isInCity) {
            resolve({
              ...payload,
              landmarkName: entry
            })
          } else {
            resolve({
              ...payload,
              landmarkName: '上海',
              cityId: 15
            })
          }
        } else {
          resolve({
            ...payload,
            landmarkName: '上海',
            cityId: 15
          })
        }
      })
    })
  })
})
